﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DZTC.UselessAppEver.Core;

namespace DZTC.UselessAppEver.UI
{
    public partial class SelectFilesForm : Form
    {
        private SelectFilesController Controller { get; set; }
     

        #region Filters

        private string FilterBase32Files
        {
            get
            {
                return String.Format("{0}|*{1}|{2}|*", Localization.Base32Files,
                                     Const.FILE_EXTENSION, Localization.AllFiles);
            }
        }

        private string FilterOnlyAllFiles
        {
            get { return String.Format("{0}|*", Localization.AllFiles); }
        }


        #endregion

        public SelectFilesForm(SelectFilesController controller)
        {
            InitializeComponent();
            SetLocalization();

            Controller = controller;
         
            DialogResult = DialogResult.Cancel;
        }

        private void SetLocalization()
        {
            gbInputFile.Text = Localization.InputFile;
            gbOutputFile.Text = Localization.OutputFile;

            fdInputFile.Title = Localization.SelectInputFile;
            fdOutputFile.Title = Localization.SelectOutputFile;

            rbEncode.Text = Localization.Encode;
            rbDecode.Text = Localization.Decode;
            lbDirection.Text = Localization.Direction;

            btStart.Text = Localization.StartConversion;
        }

        public void SetDisplayingMode(DisplayingMode mode)
        {
            lbDirection.Visible = rbDecode.Visible
                                  = rbEncode.Visible = mode.In(DisplayingMode.Default);

            gbInputFile.Visible = mode.In(DisplayingMode.Default);

            gbOutputFile.Visible = mode.In(DisplayingMode.Encode,
                                           DisplayingMode.Default,
                                           DisplayingMode.Decode);


            switch (mode)
            {
                case DisplayingMode.Default:
                    Text = Localization.SelectFilesFormCaptionDefault;
                    break;

                case DisplayingMode.Encode:
                    Text = Localization.SelectFilesFormCaptionEncode;
                    break;

                case DisplayingMode.Decode:
                    Text = Localization.SelectFilesFormCaptionDecode;
                    break;

                default:
                    throw new NotImplementedException();
            }
        }

        private bool SettingDirection { get; set; }

        public void SetDirection(ConversionDirection direction)
        {
            SettingDirection = true;

            rbDecode.Checked = direction == ConversionDirection.Decode;
            rbEncode.Checked = direction == ConversionDirection.Encode;
        
            if (direction == ConversionDirection.Decode)
            {
                fdInputFile.Filter = FilterBase32Files;
                fdOutputFile.Filter = FilterOnlyAllFiles;
            }
            else
            {
                fdInputFile.Filter = FilterOnlyAllFiles;
                fdOutputFile.Filter = FilterBase32Files;
            }

            SettingDirection = false;
        }

        private void DirectionChange(object sender, EventArgs e)
        {
            if (SettingDirection) return;

            Controller.ChangeDirection(rbDecode.Checked
                                           ? ConversionDirection.Decode
                                           : ConversionDirection.Encode);
        }

        public void SetInputFile(string fileName)
        {
            tbInputFile.Text = fileName;
            fdInputFile.FileName = fileName;

            try
            {
                fdInputFile.InitialDirectory = Path.GetDirectoryName(fileName);
            }
            catch (ArgumentException)
            {
            }
        }


        public void SetOutputFile(string fileName)
        {
            tbOutputFile.Text = fileName;
            fdOutputFile.FileName = fileName;

            try
            {
                fdOutputFile.InitialDirectory = Path.GetDirectoryName(fileName);
            }
            catch (ArgumentException)
            {
            }
        }

        private void BrowseOutputFile(object sender, EventArgs e)
        {
            if (fdOutputFile.ShowDialog() == DialogResult.OK)
            {
                Controller.SetOutputFileName(fdOutputFile.FileName);
            }
        }

        private void BrowseInputFile(object sender, EventArgs e)
        {
            if (fdInputFile.ShowDialog() == DialogResult.OK)
            {
                Controller.SetInputFileName(fdInputFile.FileName);
            }
        }


        

        

        private void StartClick(object sender, EventArgs e)
        {
            Controller.SetInputFileName(tbInputFile.Text);
            Controller.SetOutputFileName(tbOutputFile.Text);

            Controller.Start();
        }

        public void CloseOk()
        {
            DialogResult = DialogResult.OK;
            Close();
        }

     
    }
}
